<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:circularProgressBar="clr-namespace:MaterialDesignThemes.Wpf.Converters.CircularProgressBar;assembly=MaterialDesignThemes.Wpf"
                    xmlns:transitions="clr-namespace:MaterialDesignThemes.Wpf.Transitions;assembly=MaterialDesignThemes.Wpf"
                    xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf">

  <Style x:Key="MaterialDesignLinearProgressBar" TargetType="{x:Type ProgressBar}">
    <Setter Property="Foreground" Value="{DynamicResource PrimaryHueDarkBrush}"/>
    <Setter Property="Background" Value="{DynamicResource PrimaryHueLightBrush}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueLightBrush}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Height" Value="4"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ProgressBar}">
          <ControlTemplate.Resources>
            <Storyboard x:Key="OnLoaded">
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="TemplateRoot">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
              </DoubleAnimationUsingKeyFrames>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="TemplateRoot">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
              </DoubleAnimationUsingKeyFrames>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="TemplateRoot">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="OnLoadedNoAnimation">
              <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="TemplateRoot" To="1" Duration="0" />
              <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="TemplateRoot" To="1" Duration="0"/>
              <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="TemplateRoot" To="1" Duration="0"/>
            </Storyboard>
          </ControlTemplate.Resources>
          <Grid x:Name="TemplateRoot" RenderTransformOrigin="0,0.5" Opacity="0">
            <Grid.RenderTransform>
              <TransformGroup>
                <ScaleTransform ScaleX="0" ScaleY="0" />
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
              </TransformGroup>
            </Grid.RenderTransform>
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Determinate"/>
                <VisualState x:Name="Indeterminate">
                  <Storyboard RepeatBehavior="Forever">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="Animation">
                      <EasingDoubleKeyFrame KeyTime="0" Value="0.25"/>
                      <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.25"/>
                      <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.25"/>
                    </DoubleAnimationUsingKeyFrames>
                    <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="Animation">
                      <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5"/>
                      <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
                      <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5"/>
                    </PointAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"/>
            <Rectangle x:Name="PART_Track"/>
            <Grid x:Name="PART_Indicator" ClipToBounds="true" HorizontalAlignment="Left">
              <Rectangle x:Name="Indicator" Fill="{TemplateBinding Foreground}"/>
              <Rectangle x:Name="Animation" Fill="{TemplateBinding Foreground}" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                  <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                  </TransformGroup>
                </Rectangle.RenderTransform>
              </Rectangle>
            </Grid>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="transitions:TransitionAssist.DisableTransitions" Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource OnLoadedNoAnimation}" Name="BeginStoryboardOnLoadedNoAnimation" />
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="BeginStoryboardOnLoadedNoAnimation" />
              </Trigger.ExitActions>
            </Trigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsVisible" Value="True" />
                <Condition Property="transitions:TransitionAssist.DisableTransitions" Value="False" />
              </MultiTrigger.Conditions>
              <MultiTrigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource OnLoaded}" Name="BeginStoryboardOnLoaded" />
              </MultiTrigger.EnterActions>
              <MultiTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="BeginStoryboardOnLoaded" />
              </MultiTrigger.ExitActions>
            </MultiTrigger>
            <Trigger Property="Orientation" Value="Vertical">
              <Setter Property="LayoutTransform">
                <Setter.Value>
                  <RotateTransform Angle="-90"/>
                </Setter.Value>
              </Setter>
            </Trigger>
            <Trigger Property="IsIndeterminate" Value="true">
              <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <circularProgressBar:StartPointConverter x:Key="StartPointConverter" />
  <circularProgressBar:ArcSizeConverter x:Key="ArcSizeConverter" />
  <circularProgressBar:ArcEndPointConverter x:Key="ArcEndPointConverter" />
  <circularProgressBar:LargeArcConverter x:Key="LargeArcConverter" />
  <circularProgressBar:RotateTransformCentreConverter x:Key="RotateTransformCentreConverter" />
  <converters:NotZeroConverter x:Key="NotZeroConverter" />

  <Style x:Key="MaterialDesignCircularProgressBar" TargetType="{x:Type ProgressBar}">
    <Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Width" Value="20" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ProgressBar}">
          <ControlTemplate.Resources>
            <Storyboard x:Key="IsIndeterminateStoryboard" TargetName="RotateTransform" TargetProperty="Angle" RepeatBehavior="Forever">
              <DoubleAnimation From="0" To="359" Duration="0:0:2" />
            </Storyboard>
            <Storyboard x:Key="IsFullyIndeterminateScaleStoryboard">
              <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FullyIndeterminateGridScaleTransform"
                                             Storyboard.TargetProperty="ScaleX"
                                             RepeatBehavior="Forever">
                <SplineDoubleKeyFrame KeyTime="0" Value="0.0" />
                <SplineDoubleKeyFrame KeyTime="0:0:1" Value="1.0" />
                <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0.0" />
              </DoubleAnimationUsingKeyFrames>
              <DoubleAnimation Storyboard.TargetName="RotateTransform"
                               Storyboard.TargetProperty="Angle"
                               RepeatBehavior="Forever"
                               From="00" To="359" Duration="0:0:1.25" />
            </Storyboard>
          </ControlTemplate.Resources>
          <Grid x:Name="TemplateRoot" ClipToBounds="False">
            <Grid x:Name="FullyIndeterminateGrid">
              <Grid.RenderTransform>
                <ScaleTransform x:Name="FullyIndeterminateGridScaleTransform" ScaleX="0" />
              </Grid.RenderTransform>
            </Grid>
            <Grid x:Name="PathGrid" Margin="2" />
            <Canvas>
              <Ellipse Fill="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
              <Path x:Name="Path" Stroke="{TemplateBinding Foreground}" StrokeThickness="3"
                    Canvas.Top="2" Canvas.Left="2"
                    RenderTransformOrigin="0, 0">
                <Path.Data>
                  <PathGeometry>
                    <PathFigure StartPoint="{Binding ElementName=PathGrid, Path=ActualWidth, Converter={StaticResource StartPointConverter}, Mode=OneWay}">
                      <ArcSegment Size="{Binding ElementName=PathGrid, Path=ActualWidth, Converter={StaticResource ArcSizeConverter}, Mode=OneWay}"
                                  SweepDirection="Clockwise">
                        <ArcSegment.IsLargeArc>
                          <MultiBinding Converter="{StaticResource LargeArcConverter}">
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value" />
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Minimum" />
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum" />
                            <Binding ElementName="FullyIndeterminateGridScaleTransform" Path="ScaleX" />
                          </MultiBinding>
                        </ArcSegment.IsLargeArc>
                        <ArcSegment.Point>
                          <MultiBinding Converter="{StaticResource ArcEndPointConverter}">
                            <Binding ElementName="PathGrid" Path="ActualWidth" />
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value" />
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Minimum" />
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum" />
                            <Binding ElementName="FullyIndeterminateGridScaleTransform" Path="ScaleX" />
                          </MultiBinding>
                        </ArcSegment.Point>
                      </ArcSegment>
                    </PathFigure>
                  </PathGeometry>
                </Path.Data>
                <Path.RenderTransform>
                  <TransformGroup>
                    <RotateTransform x:Name="RotateTransform"
                                     CenterX="{Binding ElementName=PathGrid, Path=ActualWidth, Converter={StaticResource RotateTransformCentreConverter}, Mode=OneWay}"
                                     CenterY="{Binding ElementName=PathGrid, Path=ActualWidth, Converter={StaticResource RotateTransformCentreConverter}, Mode=OneWay}" />
                  </TransformGroup>
                </Path.RenderTransform>
              </Path>
            </Canvas>
          </Grid>
          <ControlTemplate.Triggers>
            <MultiDataTrigger>
              <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsIndeterminate, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding IsVisible, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding Value, RelativeSource={RelativeSource Self}, Converter={StaticResource NotZeroConverter}}" Value="True" />
              </MultiDataTrigger.Conditions>
              <MultiDataTrigger.EnterActions>
                <!--<RemoveStoryboard BeginStoryboardName="IsFullyIndeterminateStoryboard" />-->
                <BeginStoryboard Storyboard="{StaticResource IsIndeterminateStoryboard}"
                                 Name="IsIndeterminateStoryboard"/>
              </MultiDataTrigger.EnterActions>
              <MultiDataTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="IsIndeterminateStoryboard" />
              </MultiDataTrigger.ExitActions>
            </MultiDataTrigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsIndeterminate" Value="True" />
                <Condition Property="Value" Value="0" />
                <Condition Property="IsVisible" Value="True" />
              </MultiTrigger.Conditions>
              <MultiTrigger.EnterActions>
                <!--<RemoveStoryboard BeginStoryboardName="IsIndeterminateStoryboard" />-->
                <BeginStoryboard Storyboard="{StaticResource IsFullyIndeterminateScaleStoryboard}"
                                 Name="IsFullyIndeterminateStoryboard"/>
              </MultiTrigger.EnterActions>
              <MultiTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="IsFullyIndeterminateStoryboard" />
              </MultiTrigger.ExitActions>
            </MultiTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Type ProgressBar}" BasedOn="{StaticResource MaterialDesignLinearProgressBar}" TargetType="{x:Type ProgressBar}" />

</ResourceDictionary>
